using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class Slingshot : MonoBehaviour
{
    private Transform pointLeft;
    private Transform pointRight;
    private Transform pointCenter;
    private LineRenderer lineRendererLeft;
    private LineRenderer lineRendererRight;

    public float MaxDistance { get; private set; } = 5.0f;
    private bool drawing = false;
    private Transform transformBird;

    public Vector3 CenterPointPosition => this.pointCenter.position;

    void Awake()
    {
        this.pointLeft = transform.Find(Defines.LeftPoint);
        this.pointRight = transform.Find(Defines.RightPoint);
        this.pointCenter = transform.Find(Defines.CenterPoint);
        this.lineRendererLeft = transform.Find(Defines.LeftLine).GetComponent<LineRenderer>();
        this.lineRendererRight = transform.Find(Defines.RightLine).GetComponent<LineRenderer>();
        this.ToggleLines(false);
    }

    void Start()
    {
        
    }

    void Update()
    {
        if(this.drawing)
        {
            this.DrawLines();
        }
    }

    public void StartDraw(Transform transformBird)
    {
        this.drawing = true;
        this.transformBird = transformBird;
        this.ToggleLines(true);
    }

    public void EndDraw()
    {
        this.drawing = false;
        this.ToggleLines(false);
    }

    private void DrawLines()
    {
        var birdPosition = (this.transformBird.position - this.pointCenter.position).normalized * 0.35f + this.transformBird.position;
        this.lineRendererLeft.SetPosition(0, birdPosition);
        this.lineRendererRight.SetPosition(0, birdPosition);
        this.lineRendererLeft.SetPosition(1, pointLeft.position);
        this.lineRendererRight.SetPosition(1, pointRight.position);
    }
    
    private void ToggleLines(bool enabled)
    {
        this.lineRendererLeft.enabled = enabled;
        this.lineRendererRight.enabled = enabled;
    }
}
